const MyPromise = require('./promise')
const { resolve, reject } = require('./promise')

// basic
let p1 = new MyPromise((resolve, reject) => {
    setTimeout(()=> {
        resolve('hello')
        reject('world')
    },0)
})

let p2 = new MyPromise((resolve, reject) => {
    setTimeout(()=> {
        reject('world')
        resolve('hello')
    },0)
})

p1.then(console.log, console.log)
p2.then(console.log, console.log)

// then

let p3 = new MyPromise((resolve, reject) => {
    setTimeout(()=> {
        resolve('hello p3')
    },0)
})

p3.then(console.log, console.log)
p3.then(console.log, console.log)
p3.then(console.log, console.log)
p3.then().then(1).then(2).then({}).then(new MyPromise(resolve=>resolve(1))).then(console.log)

var p4 = p3.then((v)=> {
    return p4
})

p4.then(console.log, error => console.log(error.message))


let p5 = new MyPromise((resolve, reject) => {
    setTimeout(()=> {
        resolve('hello p5')
    },0)
}).then(()=> {
    return new MyPromise((resolve, reject) => {
        resolve('hello p5 then')
    })
})

p5.then(console.log, console.log)

let p6 = new MyPromise((resolve, reject) => {
    setTimeout(()=> {
        resolve('hello p6')
    },0)
    // throw 'exec error'
}).then(()=> {
    // throw 'then error'
    return new MyPromise((resolve, reject) => {
        resolve('hello p6 then')
    })
}, console.log)

p6.then(console.log, console.log)



// catch
let p7 = new MyPromise((resolve, reject) => {
    setTimeout(()=> {
        resolve('hello p7')
    },0)
    // throw 'exec error'
}).then(()=> {
    // throw 'then error'
    return new MyPromise((resolve, reject) => {
        resolve('hello p7 then')
    })
}).catch(console.log)


let p8 = new MyPromise((resolve, reject) => {
    setTimeout(()=> {
        resolve('hello p8')
    },0)
}).then(()=> {
    return new MyPromise((resolve, reject) => {
        reject('hello p8 then')
    })
}).then(v => console.log('resolved ' + v), v => console.log('rejected ' + v))

// resolve

console.log(MyPromise.resolve(1))

// node 环境测试发现Promise是pending状态，而web环境是fulfilled转改，存在差异？
console.log(MyPromise.resolve({a:1, b:2, then: (resolve, reject) => resolve('thenable')}))

let p = new MyPromise((resolve, reject)=> reject('reject'))
console.log(MyPromise.resolve(p) === p)


// reject

console.log(MyPromise.reject(1))
console.log(MyPromise.reject(new MyPromise(resolve=>resolve(1))))

// finally

new MyPromise((resolve, reject)=>{
    throw 'error'
}).finally(()=> {
    console.log('finally')
})

// all

console.log('------------------all-----------------')
MyPromise.all('ssss').then(console.log)
MyPromise.all([1, 2, null, undefined]).then(console.log)
MyPromise.all([1, 2, new MyPromise((resolve, reject)=> reject('reject'))]).then(console.log, console.log)


// race

console.log('------------------race-----------------')
MyPromise.race('ssss').then(console.log)
MyPromise.race([1, 2, null, undefined]).then(console.log)
MyPromise.race([new MyPromise((resolve, reject)=> {
    setTimeout(()=> {
        reject('race reject')
    }, 1001)
}), new MyPromise((resolve, reject)=> {
    setTimeout(()=> {
        resolve('race resolve')
    }, 200)
})]).then(console.log, console.log)

// any

console.log('------------------any-----------------')
MyPromise.any('ssss').then(console.log)
MyPromise.any([1, 2, null, undefined]).then(console.log)
MyPromise.any([new MyPromise((resolve, reject)=> reject('any reject 1')), new MyPromise((resolve, reject)=> reject('any reject 2'))]).then(console.log, console.log)


// allSettled

console.log('------------------allSettled-----------------')
MyPromise.allSettled('ssss').then(console.log)
MyPromise.allSettled([1, 2, null, undefined]).then(console.log)
MyPromise.allSettled([1, 2, new MyPromise((resolve, reject)=> reject('reject'))]).then(console.log, console.log)
